package Controller;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import Model.Category;
import Model.HibernateUtils;

public class ControladorCategoria {
	public void insertar(String descripcion, String nombreCategoria) {
		Session session = HibernateUtils.SESSION_FACTORY.openSession();
		session.beginTransaction();
		Category cat = new Category();
		cat.setDescripcion(descripcion);
		cat.setNombreC(nombreCategoria);
		session.save(cat);
		session.getTransaction().commit();
		session.close();
	}

	public void Eliminar(Category cat) {
		Session session = HibernateUtils.SESSION_FACTORY.openSession();
		session.beginTransaction();// beginning of the transaction
		session.delete(cat);
		session.getTransaction().commit();// end of transaction
		session.close();// end of session

	}

	public List<Category> cargarTodos() {
		Session session = HibernateUtils.SESSION_FACTORY.openSession();
		List<Category> lst = session.createCriteria(Category.class).list();
		session.close();// end of session

		return lst;
	}

	public void Actualizar(Category cat) {
		Session session = HibernateUtils.SESSION_FACTORY.openSession();
		session.beginTransaction();
		session.saveOrUpdate(cat);
		session.getTransaction().commit();
		session.close();
	}
}
